// pages/feedback/index.js
const baseUrl = require('../../config').baseUrl
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        feedbackText: '',// 反馈文字
        updateImgList: [] // 图片上传列表
    },
    chooseImage () {
        let _this = this
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success (res) {
                // tempFilePath可以作为img标签的src属性显示图片
                let imgUrl = res.tempFilePaths[0]
                wx.uploadFile({
                    url: baseUrl,
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    header: {
                        "content-type": "application/x-www-form-urlencoded"
                    },
                    formData: {
                        data: JSON.stringify({
                            business: "WxUploadFile",
                            data: app.globalData.account
                        })
                    },
                    success (res) {
                        res.data = JSON.parse(res.data)
                        if (res.data.code === 0) {
                            _this.setData({
                                "updateImgList": _this.data.updateImgList.concat({ imgUrl: imgUrl, fileid: res.data.fileid })
                            })
                        }
                    }
                })
            }
        })
    },
    /**
     * 删除上传的图片
     */
    deleteImg (e) {
        this.data.updateImgList.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            "updateImgList": this.data.updateImgList
        })
    },
    /**
    * 预览上传图片
    */
    previewImg: function (e) {
        var currentSrc = e.currentTarget.dataset.src
        var imgArr = []
        this.data.updateImgList.forEach(item => {
            imgArr.push(item.imgUrl)
        });
        wx.previewImage({
            current: currentSrc,     //当前图片地址
            urls: imgArr
        })
    },
    inputTextarea (e) {
        if (e.detail.value.length > 100) {
            this.setData({
                feedbackText: e.detail.value.substr(0, 100)
            })
        } else {
            this.setData({
                feedbackText: e.detail.value
            })
        }
    },
    saveFeedBack () {
        if (this.data.feedbackText === '') {
            wx.showToast({
                title: '请输入反馈意见',
                icon: 'none',
                duration: 1000
            })
            return false
        }
        let This = this
        let feedbackimgs = ''
        this.data.updateImgList.forEach(item => {
            if (feedbackimgs) {
                feedbackimgs += `,${item.fileid}`
            } else {
                feedbackimgs += item.fileid
            }
        })
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "SaveFeedback",
                    data: {
                        account: app.globalData.account,
                        content: this.data.feedbackText,
                        feedbackimgs: feedbackimgs
                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    wx.showToast({
                        title: '反馈成功',
                        icon: 'none',
                        duration: 1000
                    })
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 1000)
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    }
})